package com.maple.common.widget.filter

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.core.graphics.drawable.toDrawable
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.maple.common.R
import com.maple.common.base.BaseQuickBindAdapter
import com.maple.common.databinding.PopupFilterWindowBinding
import com.maple.common.ext.divider
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px


/**
 * Tip:
 * Create by SeVen on 2024/3/30 22:07
 */
class FilterPopupWindow<T : Any, VH : RecyclerView.ViewHolder> @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : PopupWindow(context, attrs, defStyleAttr) {

    private var mBinding: PopupFilterWindowBinding =
        PopupFilterWindowBinding.inflate(LayoutInflater.from(context))

    private var activity: Activity? = null

    init {
        this.isFocusable = true
        this.isOutsideTouchable = true
        this.contentView = mBinding.root
        this.setBackgroundDrawable(Color.RED.toDrawable())
        this.update()
        activity = context as Activity
        initData(context)
    }

    private fun initData(context: Context) {
        mBinding.rvPop.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            divider(context.getColor(R.color.common_underline_color))
            setHasFixedSize(true)
            isNestedScrollingEnabled = false
        }
    }


    fun setAdapter(
        filterAdapter: BaseQuickBindAdapter<T, VH>,
    ) {
        mBinding.rvPop.adapter = filterAdapter
    }

    fun setFilterHeight(itemCount: Int) {
        this.height = dp2px(48 * itemCount.toFloat())
    }


    fun showPopupWindow(parent: View) {
        if (!this.isShowing) {
            this.showAsDropDown(parent, parent.layoutParams.width, 0)
            val lp: WindowManager.LayoutParams? = activity?.window?.attributes
            lp?.alpha = 0.7f
            activity?.window?.attributes = lp
        } else {
            this.dismiss()
        }
    }

    fun showPopupWindow(parent: View, gravity: Int) {
        if (!this.isShowing) {
            this.showAtLocation(parent, gravity, 0, 0)
            this.showAsDropDown(parent, parent.layoutParams.width, 0)
            val lp: WindowManager.LayoutParams? = activity?.window?.attributes
            lp?.alpha = 0.7f
            activity?.window?.attributes = lp
        } else {
            this.dismiss()
        }
    }

    override fun dismiss() {
        super.dismiss()
        val lp = activity!!.window.attributes
        lp.alpha = 1f
        activity!!.window.attributes = lp
    }

    fun dismissPopupWindow() {
        if (this.isShowing) {
            this.dismiss()
        }
    }
}